<?php
namespace app\admin666\controller;

use think\Db;
use think\Validate;

class Notice extends Admin
{
    /**
    * 代理公告
    * @date: 2018年6月7日 上午10:26:48
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function index(){
	    $map = [];
	    $q = input('get.q','');
	    if($q != ''){
	    	$map['title'] = ['like',"%{$q}%"];
	    }
		$order="id desc";
		
		$list = Db::name('notice')->where($map)->paginate(15);
		$page = $list->render();
		
		$this->assign('_list', $list);
		$this->assign('_page', $page);
		
		return $this->fetch();
    }
    
    /**
    * 添加公告
    * @date: 2018年6月7日 上午10:32:54
    * @author: xiaocui
    * @param: $GLOBALS
    * @return:
    */
    public function add(){
        if(request()->isPost()){
            $rule = [
                'title' =>  'require',
                'content' =>  'require'
            ];
            
            $msg = [
                'title.require' => '请填写公告标题',
                'content.require' => '请填写公告内容',
            ];
            $validate = new Validate($rule,$msg);
            
            if(!$validate->check(input('post.'))){
                $this->error($validate->getError());
            }
            
            $model = model('Notice');
            $data = input('post.');
            if($model->save($data)){
                $this->success('添加成功！','');
            }else{
                $this->error($model->getError());
            }
    
        }else{
            return $this->fetch('edit');
        }
    }
    
    /**
    * 编辑分组
    * @date: 2018年6月7日 上午10:50:46
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function edit($id=0){
        if(request()->isPost()){
            $rule = [
                'title' =>  'require',
                'content' =>  'require'
            ];
            
            $msg = [
                'title.require' => '请填写公告标题',
                'content.require' => '请填写公告内容',
            ];
            $validate = new Validate($rule,$msg);
            
            $model = model('Notice');
            $data = input('post.');
            if($model->save($data,['id'=>$id])){
                $this->success('编辑成功！','');
            }else{
                if($model->getError()){
                    $this->error($model->getError());
                }else{
                    $this->success('编辑成功！','');
                }
            }
        }else{
            $info = model('Notice')->find($id);
            $this->assign('info',$info);
            return $this->fetch();
        }
    }
    
    /**
    * 删除分组
    * @date: 2018年6月7日 上午10:50:56
    * @author: xiaocui
    * @param: $GLOBALS
    * @return:
    */
    public function del($id=0){
        $model = model('Notice');
        if(($model->destroy($id))){
            //记录行为
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
}
